# Replication archive for: Coppock, Alexander and Seth J. Hill and Lynn Vavreck. 2020. "The Small Effects of Political Advertising are Small Regardless of Context, Message, Sender, or Receiver: Evidence from 59 Real-time Randomized Experiments" Science Advances, forthcoming.

library(tidyverse)
library(estimatr)
source("helpers.R")

exps <- read_rds("exps.rds")
ad_df <- read_csv("ad_df.csv")

general <- exps %>% filter(election_phase == "General Election") 

CATEs <-
  bind_rows(
    general %>% group_by(pid_3_pre, battleground) %>% do(estimate_ad_effects(., "general_vote_HC")),
    general %>% group_by(pid_3_pre, battleground) %>% do(estimate_ad_effects(., "general_vote_DT"))
  ) %>%
  filter(grepl("ad_id_fac", term)) %>% 
  mutate(ad_id = as.numeric(gsub("ad_id_fac", "", term))) %>%
  left_join(ad_df) %>%
  ungroup %>%
  mutate(
    target_candidate = (
      outcome == "general_vote_DT" & ad_valence %in% c("Pro Trump", "Anti Trump") |
      outcome == "general_vote_HC" & ad_valence %in% c("Pro Clinton", "Anti Clinton")
    ),
    attacking_candidate = 
      (outcome == "general_vote_DT" & ad_valence %in% c("Anti Clinton") |
         outcome == "general_vote_HC" & ad_valence %in% c("Anti Trump")),
    estimate_type = case_when(
      target_candidate ~ "target_candidate",
      attacking_candidate ~ "attacking_candidate",
      TRUE ~ NA_character_
    ),
    attack = as.numeric(grepl("Anti", ad_valence)),
    general = as.numeric(date > as.Date("2016-06-10")),
    pro_clinton = as.numeric(ad_valence == "Pro Clinton"),
    pro_sanders = as.numeric(ad_valence == "Pro Sanders"),
    pro_cruz = as.numeric(ad_valence == "Pro Cruz"),
    pro_kasich = as.numeric(ad_valence == "Pro Kasich"),
    pro_trump = as.numeric(ad_valence == "Pro Trump"),
    
    anti_clinton = as.numeric(ad_valence == "Anti Clinton"),
    anti_trump = as.numeric(ad_valence == "Anti Trump"),
    date_numeric = as.numeric(date) / 31,
    attack_d = attack - mean(attack),
    general_d = general - mean(general),
    
    pro_clinton_d = pro_clinton - mean(pro_clinton),
    pro_trump_d = pro_trump - mean(pro_trump),
    anti_clinton_d = anti_clinton - mean(anti_clinton),
    anti_trump_d = anti_trump - mean(anti_trump),
    
    pac = if_else(ad_sponsor %in% c("clinton", "cruz", "sanders", "trump"), 0, 1),
    pac_d = pac - mean(pac),
    
    date_d = date_numeric - mean(date_numeric),
    attack_general_d = attack_d*general_d - mean(attack_d*general_d),
    attack_date_d = attack_d*date_d - mean(attack_d*date_d),
    
    pro_democrat = as.numeric(ad_valence %in% c("Anti Trump", "Pro Clinton")),
    pro_republican = as.numeric(ad_valence %in% c("Pro Trump", "Anti Clinton")),
    
    democrat = as.numeric(pid_3_pre == "Democrat"),
    republican = as.numeric(pid_3_pre == "Republican"),
    independent = as.numeric(pid_3_pre == "Independent"),
    
    attack_d = attack - mean(attack),
    date_d = date_numeric - mean(date_numeric),
    attack_date_d = attack_d*date_d - mean(attack_d*date_d),
    
    pro_democrat_d = pro_democrat - mean(pro_democrat),
    pro_republican_d = pro_republican - mean(pro_republican),
    democrat_d = democrat - mean(democrat),
    republican_d = republican - mean(republican),
    independent_d = independent - mean(independent),
    
    battleground_d = battleground - mean(battleground),
    
    ad_match = 
      case_when(
        democrat == 1 & pro_democrat == 1 ~ "Democratic respondents, pro-Democratic ad",
        democrat == 1 & pro_republican == 1 ~ "Democratic respondents, pro-Republican ad",
        independent == 1 & pro_democrat == 1 ~ "Independent respondents, pro-Democratic ad",
        independent == 1 & pro_republican == 1 ~ "Independent respondents, pro-Republican ad",
        republican == 1 & pro_democrat == 1 ~ "Republican respondents, pro-Democratic ad",
        republican == 1 & pro_republican == 1 ~ "Republican respondents, pro-Republican ad"
      )
  )
# The recode happens NOW

vote_choice_df <- 
  CATEs %>% 
  filter(target_candidate) %>%
  mutate(
    estimate = if_else(attack == 1, estimate * -1, estimate),
    conf.low = if_else(attack == 1, conf.low * -1, conf.low),
    conf.high = if_else(attack == 1, conf.high * -1, conf.high),
  )

write_rds(vote_choice_df, path = "vote_choice_cates.rds")
